<?php
//If first time in page
if (!isset($_GET['f'])) {
 echo "
<h1>Setup of the LIND system</h1>
<p>Welcome to setup LIND. Setup will check the required librarys and create tables to database. Please check your settings in the settings_connection.php file! If you have issues with setting up the MySQL server, see Readme.php</p>
<p>Give the password of the usergroup:</p>
<form action='Setup.php?f=check' method='post'>
 <input type='password' name='password' maxlength='30' />
 <input type='submit' value='->' />
</form>
 ";
}

//If user coming from form
elseif ($_GET['f'] == "check") {
 require('../includes/connection_settings.php');

 //Strips " and ' marks
 $bannedMarks = array("\"", "'");

 $_POST['password'] = str_replace($bannedMarks, "", $_POST['password']);


 echo "<p>Connected to the MySQL server succesfully</p>";
 echo "<p>Choosed the right database succesfully</p>";

 //Password check
 if ($password != $_POST['password']) {
  echo "<p><b>Wrong password! Setup will not continue</b></p>";
  exit;
 }

 if (!function_exists('gd_info')) {
  echo "<p><b>Can't find QD library, you won't see the progress bar</b></p>";
 }

 else {
  $qdTable = gd_info();
  echo "<p>QD library found. Version of the QD library is {$qdTable['GD Version']}. ";

  if (!$qdTable['PNG Support']) {
   echo "<b>Can't find the support of the PNG image format of the QD library. You won't see the progress bar</b></p>";
  }

  echo "Found the support of the PNG image format of the QD library</p>";
 }


 echo "<p>Start of the modification of the database...</p>";

 if (!mysql_query("CREATE TABLE users (username TEXT, usernamePlusPassword TEXT, startingLanguage TEXT, wordCount INT(2), admin BOOLEAN)")) {
  if (!mysql_query("SELECT * FROM users")) {
   echo "<p><b>Can't create the wanted table in the database!" . mysql_error($connection) . ". Setup will not continue</b></p>";
   exit;
  }
  else {
   echo "<p><b>Warning: Table <i>Users</i> exists already. Please Check it!</b></p>";
  }
 }
 else {
  echo "<p>Created table <i>Users</i></p>";
 }

 if (!mysql_query("CREATE TABLE words (local TEXT, theForeign TEXT, creator TEXT, dictionary TEXT)")) {
  if (!mysql_query("SELECT * FROM words")) {
   echo "<p><b>Can't create the wanted table in the database!" . mysql_error($connection) . ". Setup will not continue</b></p>";
   exit;
  }
  else {
   echo "<p><b>Warning: Table <i>Words</i> exists already. Please Check it!</b></p>";
  }
 }
 else {
  echo "<p>Created table <i>Words</i></p>";
 }

 if (!mysql_query("CREATE TABLE choices (dictionary TEXT)")) {
  if (!mysql_query("SELECT * FROM choices")) {
   echo "<p><b>Can't create the wanted table in the database!" . mysql_error($connection) . ". Setup will not continue</b></p>";
   exit;
  }
  else {
   echo "<p><b>Warning: Table <i>Choices</i> exists already. Please Check it!</b></p>";
  }
 }
 else {
  echo "<p>Created table <i>Choices</i></p>";
 }

 echo "<p><b>Setup completed succesfully! You can delete this file and continue to </b><a href='../index.php'>LIND</a></b></p>";

 mysql_close($connection);
}

?>